import {
  Body,
  Controller,
  HttpCode,
  HttpException,
  HttpStatus,
  Post,
  Request,
} from '@nestjs/common';
import { Public } from './auth.decorator';
import { AuthService } from './auth.service';

@Controller('auth')
export class AuthController {
  constructor(private authService: AuthService) {}
  @Public()
  @HttpCode(HttpStatus.OK)
  @Post('register')
  async signIn(@Body() signInDto: Record<string, any>) {
      try {
         await this.authService.Rregister({
          username: signInDto.username,
          password: signInDto.password,
        });
        return {
          code:200,
          data:{
            message:'注册成功',
          }
        }
      } catch (e) {
        if (e.meta.target.includes('User_username_key')) {
          throw new HttpException('用户名称已存在', 400);
        }
      }
  }
  @Public()
  @HttpCode(HttpStatus.OK)
  @Post('login')
  login(@Body() signInDto: Record<string, any>) {
    console.log(signInDto);
    return this.authService.signIn(signInDto.username, signInDto.password);
  }
  @Post('profile')
  getProfile(@Request() req) {
    return req.user;
  }
}
